package com.truthbean.log;

/**
 * @author TruthBean
 * @since 0.0.1
 */
public final class ClassHelper {
    /**
     * default classLoader
     */
    private static ClassLoader defaultClassLoader;

    private ClassHelper() {
    }

    public static ClassLoader getDefaultClassLoader() {
        return defaultClassLoader;
    }

    public static void setDefaultClassLoader(ClassLoader defaultClassLoader) {
        ClassHelper.defaultClassLoader = defaultClassLoader;
    }

    /**
     * get class by class name
     *
     * @param className class name
     * @return class
     * @throws ClassNotFoundException if the class not found
     */
    public static Class<?> classForName(String className) throws ClassNotFoundException {
        Class<?> clazz = null;

        try {
            clazz = getClassLoader().loadClass(className);
        } catch (Exception ignored) {
        }

        if (clazz == null) {
            clazz = Class.forName(className);
        }

        return clazz;
    }

    private static ClassLoader getClassLoader() {
        return defaultClassLoader != null ? defaultClassLoader : Thread.currentThread().getContextClassLoader();
    }
}